package com.wanmait.deliverymangementsystem.usercontroller;

import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.pojo.Reviews;
import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.ReviewsService;
import com.wanmait.deliverymangementsystem.service.TaskService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserReviewsController {
    @Resource
    private   TaskService taskService;
    @Resource
    private ReviewsService reviewsService;
    @GetMapping("reviews")
    @ModelAttribute("taskId")
    public Integer reviews(Integer taskID){
            return taskID;
    }
    @PostMapping("reviews")
    public String reviews(Integer score, Integer taskId, String comment , @SessionAttribute Users user)
    {
        Reviews reviews =new Reviews();
        reviews.setTask(taskService.findByID(taskId));
        reviews.setUser(user);
        reviews.setComment(comment);
        reviews.setScore(score);
        reviewsService.createReview(reviews);
        return "redirect:/user/reviewList";
    }
    @GetMapping("reviewList")
    public  PageInfo<Reviews> reviewList(Integer page, HttpSession session, Model model){
        if (page==null)
            page=0;
      Users user= (Users) session.getAttribute("user");
      PageInfo<Reviews> reviewsPageInfo= reviewsService.findUserReviews(page,user.getId());
        model.addAttribute("reviewsPageInfo",reviewsPageInfo);
        System.out.println("reviewsPageInfo: " + reviewsPageInfo);
      return reviewsPageInfo;
    }
}
